package com.company.project.controller.ldjsc.robot;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import cn.dev33.satoken.annotation.SaCheckPermission;
import com.company.project.entity.ldjsc.robot.*;
import com.company.project.entity.ldjsc.sczt.JscScztGyzbjyysEntity;
import com.company.project.service.ldjsc.robot.JscIndexService;
import com.company.project.service.ldjsc.robot.ZyAiFixedProblemFieldService;
import com.company.project.service.ldjsc.robot.ZyAiFixedProblemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.ArrayList;
import java.util.List;
import com.company.project.common.utils.DataResult;





/**
 * 智能助手固定问题
 *
 * @author wenbin
 * @email *****@mail.com
 * @date 2025-10-22 09:33:45
 */
@Controller
@RequestMapping("/sxzy")
public class ZyAiFixedProblemController {
    @Autowired
    private ZyAiFixedProblemService zyAiFixedProblemService;

    @Autowired
    private ZyAiFixedProblemFieldService zyAiFixedProblemFieldService;

    @Autowired
    private JscIndexService jscIndexService;



    @ApiOperation(value = "查询问题")
    @PostMapping("/zyAiFixedProblem/question")
    @ResponseBody
    public DataResult findListByPage(@RequestBody ZyAiFixedProblemEntity zyAiFixedProblem){

        //如果type有值说明是固定问题
        if (StringUtils.isNotBlank(zyAiFixedProblem.getType())){


            //根据问题获取要查询sql的DetailName的字段
            LambdaQueryWrapper<ZyAiFixedProblemEntity> queryWrapper = Wrappers.lambdaQuery();
            queryWrapper.eq(ZyAiFixedProblemEntity::getQuestion, zyAiFixedProblem.getQuestion());
            ZyAiFixedProblemEntity zyAiFixedProblemEntity = zyAiFixedProblemService.getOne(queryWrapper);
            if (zyAiFixedProblemEntity.getId() != null){

                //返回的整个echart图数据
                EchartEntity echartEntity = new EchartEntity();

                //获取返回的echart图类型
                echartEntity.setEchartslx(zyAiFixedProblemEntity.getGraphType());//整个Echart图的格式

                //根据问题获取要查询的字段
                LambdaQueryWrapper<ZyAiFixedProblemFieldEntity> lqw = Wrappers.lambdaQuery();
                lqw.eq(ZyAiFixedProblemFieldEntity::getZyAiFixedProblemId, zyAiFixedProblemEntity.getId());
                List<ZyAiFixedProblemFieldEntity> zyAiFixedProblemFieldEntities = zyAiFixedProblemFieldService.list(lqw);


                List<EchartSectionEntity> echartSectionEntities = new ArrayList<>();
                //构建单条返回数据
                for (ZyAiFixedProblemFieldEntity zyAiFixedProblemFieldEntity : zyAiFixedProblemFieldEntities) {
                    String field = zyAiFixedProblemFieldEntity.getField();
                    LambdaQueryWrapper<JscIndexEntity> jsclqw = Wrappers.lambdaQuery();
                    jsclqw.like(JscIndexEntity::getDetailName, field);
                    jsclqw.orderByDesc(JscIndexEntity::getDataDate);
                    jsclqw.last("limit 100");
                    List<JscIndexEntity> list = jscIndexService.list(jsclqw);

                    //单条echart图数据
                    EchartSectionEntity echartSectionEntity = new EchartSectionEntity();
                    echartSectionEntity.setGraphType(zyAiFixedProblemFieldEntity.getGraphType());//图类型

                    List<String> xAxis = new ArrayList<>();
                    List<String> yAxis = new ArrayList<>();
                    String type = "";
                    for (JscIndexEntity jscIndexEntity : list) {
                        xAxis.add(jscIndexEntity.getDataDate());
                        yAxis.add(jscIndexEntity.getIndexValue());
                        type= jscIndexEntity.getUnit();
                    }
                    echartSectionEntity.setType(type);//单位
                    echartSectionEntity.setXAxis(xAxis);
                    echartSectionEntity.setYAxis(yAxis);

                    echartSectionEntities.add(echartSectionEntity);
                }
                echartEntity.setEchartssc(echartSectionEntities);

                return DataResult.success(echartEntity);

            }else {
                return DataResult.success(zyAiFixedProblem.getQuestion() + "无对应数据");
            }


        }else {
            String question = zyAiFixedProblem.getQuestion();
            String whereClause = question.substring(question.indexOf("WHERE"));
            LambdaQueryWrapper<JscIndexEntity> jsclqw = Wrappers.lambdaQuery();
            jsclqw.last(whereClause);
            List<JscIndexEntity> list = jscIndexService.list(jsclqw);
            return DataResult.success(list);
        }

    }

}
